package cn.com.infcn.spider.util;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.nlpcn.commons.lang.util.StringUtil;

public class URLUtil {

	public static void main(String[] args) {
		System.out.println(nextPage("http://www.cegov.cn/tabid/102/544pageidx/11/Default.aspx", 4, 1));
	}

	/**
	 * 获取网页的下一页
	 * 
	 * @param url
	 * @param param
	 * @param step
	 * @return
	 */
	public static String nextPage(String url, int index, int step) {

		String[] split = url.split("\\?");

		StringBuilder sb = new StringBuilder();

		String[] sp2 = split[0].split("//");

		sb.append(sp2[0] + "//");

		String[] sp3 = sp2[1].split("/");

		sb.append(sp3[0]);

		for (int i = 1; i < sp3.length; i++) {
			if (index == i) {
				sb.append("/" + (Integer.parseInt(sp3[i].trim()) + step));
			} else {
				sb.append("/" + sp3[i]);
			}
		}

		if (split.length == 2) {
			sb.append("?" + split[1]);
		}

		return sb.toString();
	}

	/**
	 * 获取网页的下一页
	 * 
	 * @param url
	 * @param param
	 * @param step
	 * @return
	 */
	public static String nextPage(String url, String param, int step) {

		String[] split = url.split("\\?");

		StringBuilder sb = new StringBuilder();

		sb.append(split[0] + "?");

		String startV = param + "=";

		for (int i = 1; i < split.length; i++) {
			for (String kv : split[i].split("&")) {
				if (kv.startsWith(startV)) {
					long v = Long.parseLong(kv.split("=")[1]) + step;
					sb.append(startV + v + "&");
				} else {
					sb.append(kv + "&");
				}
			}
		}

		if (sb.charAt(sb.length() - 1) == '&') {
			return sb.substring(0, sb.length() - 1);
		} else {
			return sb.toString();
		}
	}

	/**
	 * 正则验证
	 * 
	 * @param regex
	 * @param url
	 * 
	 * 
	 * @return
	 */
	public static boolean matches(String url, String regex) {
		return Pattern.matches(regex, url);
	}

	/**
	 * 网址抽取
	 * 
	 * @param page
	 * @param query
	 * @return
	 */
	public static Set<String> urls(Document doc, String cssQuery) {
		Set<String> result = new HashSet<>();
		Elements select = doc.getAllElements();

		if (StringUtil.isNotBlank(cssQuery)) {
			select = select.select(cssQuery);
		}

		if (select != null) {
			String url = null;
			// 解析A标签中href的链接 abs：绝对路径
			Elements es = select.select("a");
			for (Element e : es) {
				url = e.attr("abs:href");

				if (StringUtil.isBlank(url)) {
					continue;
				}

				if (url.startsWith("mailto:")) {
					continue;
				}
				url = url.replaceAll("#.*+$", "");
				result.add(url);
			}

			// 解析Iframe标签中src链接
			Elements esIframe = select.select("iframe");
			for (Element e : esIframe) {
				url = e.attr("abs:src");

				if (StringUtil.isBlank(url)) {
					continue;
				}

				if (url.startsWith("mailto:")) {
					continue;
				}
				url = url.replaceAll("#.*+$", "");
				result.add(url);
			}
			
//			// 解析Iframe标签中src链接
//			Elements esImg = select.select("img");
//			for (Element e : esImg) {
//				url = e.attr("abs:src");
//
//				if (StringUtil.isBlank(url)) {
//					continue;
//				}
//
//				if (url.startsWith("mailto:")) {
//					continue;
//				}
//				url = url.replaceAll("#.*+$", "");
//				result.add(url);
//			}
		}
		return result;
	}
	
	public static String extracting(Document doc, String cssQuery) {
		return doc.select(cssQuery).text();
	}

	public static String nextPageByTemplate(String url, String template, int step) {
		String[] split = template.split("\\{\\}");

		String pageNoStr = url.replace(split[0], "");

		if (split.length > 1) {
			pageNoStr = pageNoStr.replace(split[1], "");
		}

		String newUrl = split[0] + (Integer.parseInt(pageNoStr) + step);

		if (split.length > 1) {
			newUrl = newUrl + split[1];
		}

		return newUrl;
	}

}
